package com.gzy.net_app.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.widget.Toast;
import com.gzy.network.NetworkApi;
import com.orhanobut.hawk.Hawk;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : guzy
 * @program: net-app
 * @description: 自定义MyApplication
 * @date : 2022-03-24 12:52
 **/
public class MyApplication extends Application {
  private List<Activity> oList;//用于存放所有启动的Activity的集合
  static Context context;

  @Override
  public void onCreate() {
    super.onCreate();
    context = getApplicationContext();
    oList = new ArrayList<Activity>();
    Hawk.init(this).build();
    //初始化
    NetworkApi.init(new NetworkRequiredInfo(this));
  }

  public static synchronized MyApplication context() {
    return (MyApplication) context;
  }

  /**
   * Show Toast  简单封装
   */
  public static void showToastLong(String msg) {
    showToast(context, msg, Toast.LENGTH_LONG);
  }

  public static void showToastShort(String msg) {
    showToast(context, msg, Toast.LENGTH_SHORT);
  }

  public static void showToastShort(int strRes) {
    showToast(context, context.getString(strRes), Toast.LENGTH_SHORT);
  }

  public static void showToast(String msg) {
    showToast(context, msg, Toast.LENGTH_SHORT);
  }

  public static void showToast(int strRes) {
    showToast(context, context.getString(strRes), Toast.LENGTH_SHORT);
  }

  public static void showToastLong(int strRes) {
    showToast(context, context.getString(strRes), Toast.LENGTH_LONG);
  }

  public static void showToast(Context context, String msg, int duration) {
    Toast.makeText(context, msg, duration).show();
  }

  /**
   * 添加Activity
   */
  public void addActivity_(Activity activity) {
    // 判断当前集合中不存在该Activity
    if (!oList.contains(activity)) {
      oList.add(activity);//把当前Activity添加到集合中
    }
  }

  /**
   * 销毁单个Activity
   */
  public void removeActivity_(Activity activity) {
    //判断当前集合中存在该Activity
    if (oList.contains(activity)) {
      oList.remove(activity);//从集合中移除
      activity.finish();//销毁当前Activity
    }
  }

  /**
   * 销毁所有的Activity
   */
  public void removeALLActivity_() {
    //通过循环，把集合中的所有Activity销毁
    for (Activity activity : oList) {
      activity.finish();
    }
  }
}
